/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
define(["require", "exports", "../utils/convert"], function (require, exports, convert_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    function getSymbolKind(item, vscode) {
        switch (item.kind) {
            case 'method': return vscode.SymbolKind.Method;
            case 'enum': return vscode.SymbolKind.Enum;
            case 'function': return vscode.SymbolKind.Function;
            case 'class': return vscode.SymbolKind.Class;
            case 'interface': return vscode.SymbolKind.Interface;
            case 'var': return vscode.SymbolKind.Variable;
            default: return vscode.SymbolKind.Variable;
        }
    }
    class TypeScriptWorkspaceSymbolProvider {
        constructor(client, modeIds, vscode) {
            this.client = client;
            this.modeIds = modeIds;
            this.vscode = vscode;
        }
        provideWorkspaceSymbols(search, token) {
            return __awaiter(this, void 0, void 0, function* () {
                // typescript wants to have a resource even when asking
                // general questions so we check the active editor. If this
                // doesn't match we take the first TS document.
                let uri = undefined;
                const editor = this.vscode.window.activeTextEditor;
                if (editor) {
                    const document = editor.document;
                    if (document && this.modeIds.indexOf(document.languageId) >= 0) {
                        uri = document.uri;
                    }
                }
                if (!uri) {
                    const documents = this.vscode.workspace.textDocuments;
                    for (const document of documents) {
                        if (this.modeIds.indexOf(document.languageId) >= 0) {
                            uri = document.uri;
                            break;
                        }
                    }
                }
                if (!uri) {
                    return [];
                }
                const filepath = this.client.normalizePath(uri);
                if (!filepath) {
                    return [];
                }
                const args = {
                    file: filepath,
                    searchValue: search
                };
                const response = yield this.client.execute('navto', args, token);
                if (!response.body) {
                    return [];
                }
                const result = [];
                for (const item of response.body) {
                    if (!item.containerName && item.kind === 'alias') {
                        continue;
                    }
                    const range = convert_1.tsTextSpanToVsRange(item, this.vscode);
                    const label = TypeScriptWorkspaceSymbolProvider.getLabel(item);
                    result.push(new this.vscode.SymbolInformation(label, getSymbolKind(item, this.vscode), item.containerName || '', new this.vscode.Location(this.client.asUrl(item.file), range)));
                }
                return result;
            });
        }
        static getLabel(item) {
            let label = item.name;
            if (item.kind === 'method' || item.kind === 'function') {
                label += '()';
            }
            return label;
        }
    }
    exports.default = TypeScriptWorkspaceSymbolProvider;
});
